import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import router from './router'
import store from './store'
import './api/mock'
import Cookies from 'js-cookie'
Vue.config.productionTip = false

// 添加全局前置导航守卫
router.beforeEach((to, from, next) => {
    // 判断token存在不
    const token = Cookies.get('token')
    // token不存在,当前用户未登录,应该跳转至登录页面
    if (!token && to.name !== 'login') {
        next({ name: 'login' })
        // token存在说明用户登录
    } else if (token && to.name === 'login') {
        next({ name: 'home' })
    } else {
        next()
    }
})

Vue.use(ElementUI)
new Vue({
    router,
    store,
    render: h => h(App),
    // 解决刷新白屏
    created() {
        store.commit('addMenu', router)
    }
}).$mount('#app')
